package com.thingple.app.io;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.thingple.h5.WebViewManager;
import com.thingple.location.LocationFactory;
import com.thingple.message.MessageService;
import com.thingple.shared_context.SharedContext;
import com.thingple.uhf.RFIDDeviceManager;
import com.thingple.version.VersionManager;

/**
 * Application
 * Created by lism on 2017/8/11.
 */
public class App extends Application {

    public static App instance;

    public VersionControllHandler versionControllHandler;
    static int i = 1;
    static boolean first = true;


    @Override
    public void onCreate() {
        Log.d("#onCreate", "APP启动......");
        super.onCreate();
        instance = this;

        if (first) {
            afterCreate();
            first = false;
            Toast.makeText(this, "application start" + i++, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "application next" + i++, Toast.LENGTH_SHORT).show();
        }

    }



    private void afterCreate() {
        // 设置webviewactivity
        SharedContext.init(getApplicationContext(), "thingple.com");// 初始化配置文件
        SharedContext.shareInstance().set("licence.uri", "http://192.168.1.252:82/dm/api/code/register/");//TODO 临时

        RFIDDeviceManager.init(getApplicationContext());// 启动Device Manager
        MessageService.init(getApplicationContext());// 启动Massage Service

        WebViewManager.init(getApplicationContext());
        WebViewManager.shareInstance().comonPage = WebActivity.class;


        LocationFactory.init(getApplicationContext());
        LocationFactory.shareInstance().start();


        VersionManager.init(this);
        versionControllHandler = new VersionControllHandler(this);
    }

    public void restart() {
//        Intent i = getBaseContext().getPackageManager()
//                .getLaunchIntentForPackage(getBaseContext().getPackageName());
//        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(i);
    }

    public void alert(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
}
